package com.huiminpay.merchant.service.impl;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.merchant.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 有点儿意思
 * @Description:
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

    @Value("${sms.url}")
    String url;

    @Value("${sms.effectiveTime}")
    String effectiveTime;

    @Autowired
    RestTemplate restTemplate;


    /**
     * 发送验证码接口实现
     * @param phone 手机号
     * @return 验证码
     */
    @Override
    public String sendSms(String phone) {
        //1.拼接请求地址 url
        //"http://localhost:56085/sailing/generate?effectiveTime=600&name=sms"
        String sendUrl = url + "/generate?name=sms&effectiveTime="+effectiveTime;
        //2.定义手机号 phone
        //3.封装请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        //4.封账请求体
        HashMap<String,Object> map = new HashMap<>();
        map.put("mobile",phone);
        HttpEntity<Map<String,Object>> entity = new HttpEntity<>(map,headers);
        ResponseEntity<Map> responseEntity = null;
        //5.发送请求
        try {
             responseEntity = restTemplate.exchange(sendUrl, HttpMethod.POST, entity, Map.class);
        } catch (RestClientException e) {
            throw new RuntimeException("验证码发送失败", e);
        }
        //6.处理返回结果
        Map bodyMap = responseEntity.getBody();
        if (bodyMap == null || bodyMap.get("result")==null){
            throw new RuntimeException("验证码发送失败");
        }
        log.info("bodyMap:{}", bodyMap);
        Map mapResult = (Map) bodyMap.get("result");
        String key = (String) mapResult.get("key");
        return key;
    }

    /**
     * 请求短信服务校验验证码
     * @param verifiyKey  验证key
     * @param verifiyCode 验证码
     */
    @Override
    public void checkCode(String verifiyKey, String verifiyCode) {
        //1.封装请求路径
        //http://localhost:56085/sailing/verify?name=sms&verificationCode=665395&verificationKey=sms%3A1b264d7a83874a7ba65c4bdae94d3d9d
        String checkUrl =  url + "/verify?name=sms&verificationCode="+ verifiyCode + "&verificationKey=" + verifiyKey;
        //2.发送器请求
        ResponseEntity<Map> responseEntity = null;
        try {
             responseEntity = restTemplate.exchange(checkUrl, HttpMethod.POST, HttpEntity.EMPTY, Map.class);
        } catch (RestClientException e) {
//            throw new RuntimeException("验证码校验失败", e);
            throw new BusinessException(CommonErrorCode.E_100102);
        }
        //3.处理接口返回结果
        Map bodyMap = responseEntity.getBody();
        if (bodyMap == null || bodyMap.get("result")==null || !(Boolean) bodyMap.get("result")){
//            throw new RuntimeException("验证码校验失败");
            throw new BusinessException(CommonErrorCode.E_100102);
        }
    }
}
